package com.liy.framework.common.starter;

import com.liy.framework.common.orm.dialect.DialectFactory;
import com.liy.framework.common.orm.mybatis.interceptor.PaginationResultSetHandlerInterceptor;
import com.liy.framework.common.orm.mybatis.interceptor.PaginationStatementHandlerInterceptor;
import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

@Configuration
@EnableTransactionManagement
public class MyBatisAutoConfiguration implements TransactionManagementConfigurer {

    private static Logger log = LoggerFactory.getLogger(MyBatisAutoConfiguration.class);

    @Autowired
    DataSource dataSource;

    @Value("${mybatis.dialect-class-name: OracleDialect}")
    public void initMybatisDialect(String dialectClass) {
        DialectFactory.dialectClass = dialectClass;
    }

    /*
    * SqlSessionFactory
    */
    @Value("${mybatis.mapper-locations: classpath*:mapper/**/**.xml}")
    @Bean( name = {"sqlSessionFactory"} )
    public SqlSessionFactory sqlSessionFactory(String mapperLocations) {

        log.info(" MyBatisConfig init ...");

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(this.dataSource);  // dataSource

        // 分页结果集处理程序拦截器  Pagination ResultSet Handler Interceptor
        PaginationResultSetHandlerInterceptor paginationResultSetHandlerInterceptor = new PaginationResultSetHandlerInterceptor();

        // 分页语句处理程序拦截器  Pagination Statement Handler Interceptor
        PaginationStatementHandlerInterceptor paginationStatementHandlerInterceptor = new PaginationStatementHandlerInterceptor();

        // 设置插件
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationResultSetHandlerInterceptor, paginationStatementHandlerInterceptor});

        // 解析器
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try {
            // 设置 mapperLocations
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));

            // sqlSessionFactoryBean -> SqlSessionFactory
            return sqlSessionFactoryBean.getObject();
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }
    }



    /*
    * SqlSessionTemplate
    */
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    /*
     * PlatformTransactionManager
     */
    @Override
    @Bean("transactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(this.dataSource);
    }
}
